Skip to content

Fix user-agent register timing in C++ and Unity SDK #1217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 6, 2023

Conversation

chkuang-g
Copy link
Contributor

@chkuang-g chkuang-g commented Feb 22, 2023

Description

Provide details of the change, and generalize the change in the PR title above.

Patch user-agent register timing in C++ and add utility functions for Unity SDK to register BEFORE C++ App is created.

Several notes:

  • The original use of util::GetJNIEnvFromApp() is not reliable because it only works AFTER the first app is added.
  • The new CallAfterEnsureMethodsCached() functions is primarily for Unity SDK to register BEFORE C++ app is created, which is when the Java classes and methods are cached originally.

Testing

Describe how you've tested these changes. Link any manually triggered Integration tests or CPP binary SDK Packaging Github Action workflows, if applicable.

Integration test. Manually E2E test for Android and iOS Auth testapp.


Type of Change

Place an x the applicable box:

  • Bug fix. Add the issue # below if applicable.
  • New feature. A non-breaking change which adds functionality.
  • Other, such as a build process or documentation change.

Notes

  • Bug fixes and feature changes require an update to the Release Notes section of release_build_files/readme.md.
  • Read the contribution guidelines CONTRIBUTING.md.
  • Changes to the public API require an internal API review. If you'd like to help us make Firebase APIs better, please propose your change in a feature request so that we can discuss it together.

@chkuang-g chkuang-g added the skip-release-notes Skip release notes check label Feb 22, 2023
@chkuang-g chkuang-g force-pushed the patch/user-agent-timing branch 4 times, most recently from 73d3d33 to 9177497 Compare February 24, 2023 01:45
Change to register user-agents before platform App creation so that
the new user-agents can be captured and logged at iOS/Android level.
This utility function is used internally to ensure the callback
is triggered when all necessary Java classes and methods are cached.
@chkuang-g chkuang-g force-pushed the patch/user-agent-timing branch from 9177497 to 06bd6a2 Compare February 24, 2023 01:47
@chkuang-g chkuang-g added the tests-requested: full Trigger a FULL set of integration tests (uses expanded test matrix). label Feb 24, 2023
@github-actions github-actions bot added tests: in-progress This PR's integration tests are in progress. and removed tests-requested: full Trigger a FULL set of integration tests (uses expanded test matrix). labels Feb 24, 2023
@github-actions
Copy link

github-actions bot commented Feb 24, 2023

❌  Integration test FAILED

Requested by @chkuang-g on commit 50828a9
Last updated: Mon Mar 6 18:04 PST 2023
View integration test log & download artifacts

Failures Configs
analytics [TEST] [FAILURE] [Android] [All 3 os] [1/2 android_device: android_target]
(1 failed tests)  FirebaseAnalyticsTest.TestGetSessionID
firestore [TEST] [FLAKINESS] [Android] [1/3 os: ubuntu] [1/2 android_device: android_target]
(1 failed tests)  ValidationTest.FieldNamesMustNotBeEmpty
messaging [TEST] [FLAKINESS] [Android] [1/3 os: windows] [1/2 android_device: android_target]
(1 failed tests)  CRASH/TIMEOUT

Add flaky tests to go/fpl-cpp-flake-tracker

@github-actions github-actions bot added the tests: succeeded This PR's integration tests succeeded. label Feb 24, 2023
@firebase-workflow-trigger firebase-workflow-trigger bot removed the tests: in-progress This PR's integration tests are in progress. label Feb 24, 2023
AlmostMatt
AlmostMatt previously approved these changes Feb 27, 2023
Copy link
Contributor

@AlmostMatt AlmostMatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This generally looks good to me. I just added a few minor comments.

// Make sure the Java classes and methods are cached before triggering the
// the callback. Can be slow if this is called BEFORE any Firebase App is
// created.
void CallAfterEnsureMethodsCached(JNIEnv* env, jobject activity,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a little bit strange to me that this is implemented in app_android.cc and the header is util_android.h.

I think the implementation makes sense to be in app_android.cc as it calls CacheMethods and I guess there is not currently an app_android.h file, so I suppose this is fine as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is.

I'm not sure if it is worth adding a app_android.h just for this single function. And unfortunately the implementation has to live in app_android.cpp so that it can access to all the JNI caching and releasing functions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't the implementation in util_android, if it's not a method of App or related types like AppOptions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CallAfterEnsureMethodsCached() is an utility by nature but requires CacheMethods() and ReleaseClasses() which is only available in app_android.cpp.

https://github.com/firebase/firebase-cpp-sdk/blob/main/app/src/app_android.cc

I can think of some alternative solutions

  1. Add app_android.h and declare CallAfterEnsureMethodsCached() in app_android.h
  2. Add app_android.h and declare CacheMethods() and ReleaseClasses() so that they can be linked to util_android.cpp. Then move CallAfterEnsureMethodsCached() to util_android.cpp.
  3. Move declaration and impl of CacheMethods() and ReleaseClasses() to util_android.

Do you see a risk to implement a function declare in util_android.h but implemented in app_android.cpp?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added app_android.h

@chkuang-g chkuang-g requested a review from a-maurice March 1, 2023 19:22
@github-actions github-actions bot dismissed AlmostMatt’s stale review March 3, 2023 02:25

🍞 Dismissed stale approval on external PR.

a-maurice
a-maurice previously approved these changes Mar 3, 2023
@github-actions github-actions bot dismissed a-maurice’s stale review March 4, 2023 01:05

🍞 Dismissed stale approval on external PR.

The desktop test fail because the user-agent does not get registered
again after the app is deleted an created again.

Move the boolean to LibraryRegistry instance so that it can reset when
the app is deleted.
a-maurice
a-maurice previously approved these changes Mar 4, 2023
@github-actions github-actions bot dismissed a-maurice’s stale review March 4, 2023 03:48

🍞 Dismissed stale approval on external PR.

jonsimantov
jonsimantov previously approved these changes Mar 4, 2023
Copy link
Contributor

@jonsimantov jonsimantov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems mostly fine except for a few nits.

@@ -190,7 +190,7 @@ struct AppData {
// Tracks library registrations.
class LibraryRegistry {
private:
LibraryRegistry() {}
LibraryRegistry() : is_common_libraries_registered(false) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: what about naming this, and the accessors, "Library" instead of "Libraries", as IsCommonLibraryRegistered is more correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -277,7 +281,7 @@ static void PlatformOptionsToAppOptions(FIROptions* platform_options, AppOptions

App* App::GetInstance(const char* name) { return app_common::FindAppByName(name); }

void App::RegisterLibrary(const char* library, const char* version) {
void App::RegisterLibrary(const char* library, const char* version, void* platform_resource) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's not used on this platform, do the /* platform_resource */ thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Also changed for desktop and stub

// Make sure the Java classes and methods are cached before triggering the
// the callback. Can be slow if this is called BEFORE any Firebase App is
// created.
void CallAfterEnsureMethodsCached(JNIEnv* env, jobject activity,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't the implementation in util_android, if it's not a method of App or related types like AppOptions?

- Rename `IsCommonLibrariesRegistered` to `IsCommonLibraryRegistered`,
  and similar changes.
- Change function parameter `/* platform_resource */` for platforms
  which are not using this parameter
- Move `CallAfterEnsureMethodsCached()` declaration to `app_android.h`
@github-actions github-actions bot dismissed jonsimantov’s stale review March 6, 2023 19:40

🍞 Dismissed stale approval on external PR.

Copy link
Contributor Author

@chkuang-g chkuang-g left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change applied.

@chkuang-g chkuang-g merged commit 50828a9 into main Mar 6, 2023
@chkuang-g chkuang-g deleted the patch/user-agent-timing branch March 6, 2023 23:26
@github-actions github-actions bot added tests: in-progress This PR's integration tests are in progress. tests: failed This PR's integration tests failed. and removed tests: succeeded This PR's integration tests succeeded. labels Mar 6, 2023
@firebase-workflow-trigger firebase-workflow-trigger bot removed the tests: in-progress This PR's integration tests are in progress. label Mar 7, 2023
@firebase firebase locked and limited conversation to collaborators Apr 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
skip-release-notes Skip release notes check tests: failed This PR's integration tests failed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants